class_name EagleAttackState
extends GameplayState

@onready var eagle: Eagle = $"../../"

var attack_direction: Vector2

var animation_initial_rotation: float


func _on_enter() -> void:
    eagle.play_animation(&"attack")

    eagle.add_tag(GameplayTagLibrary.State_Air)

    attack_direction = (eagle.state_checker.player_position - eagle.global_position).normalized()

    var state_machine = get_parent() as GameplayStateMachine
    if state_machine:
        state_machine.blackboard["original_position"] = eagle.global_position

    animation_initial_rotation = eagle.animator.rotation
    eagle.animator.rotation = (attack_direction.angle() - Vector2(eagle.face_direction, 1).angle()) * -eagle.face_direction


func on_update(delta: float) -> void:
    eagle.velocity = attack_direction * eagle.get_attribute("attack_speed").current_value * delta

    if eagle.state_checker.is_ground_detected:
        change_state("ReturnState", func(): pass)
        return


func _on_exit() -> void:
    eagle.remove_tag(GameplayTagLibrary.State_Air)

    eagle.animator.rotation = animation_initial_rotation
